// console.go
package main

import (
	"fmt"
	"io"
	"log"
	"restsrv"
	"strconv"
	"strings"

	"github.com/chzyer/readline"
)

func usage(w io.Writer) {
	io.WriteString(w, `
	notify      <phone>:语音通知测试	
	callback    <[-s]phone>:-s 为回拨sip账号
	outcall     <[-w]phone>:外呼测试 -w:微众侧测试 
	asrCall     智能外呼，打开asr开关
	ttsCall     智能外呼，播放全TTS语音
	rtm 		复合接口实时tts转换测试
	transfer    <phone,phone>: 语音通知转接测试
	show        显示信息 
	setcaller   设置 主叫号码
	stopPlay    测试打断指令
	settts      设置 tts <[1.0]> 不带参数设置使用mrcp，，参数为浮点数
	setasr      设置性能测试时asr开关,默认打开
	setdelay    性能测试时设置呼叫间隔，单位ms
	setRecord   设置路音存储格式 <[mp3]>:设置为MP3，为空则恢复默认
	statics     查看当前测试结果
	clear       初始化当前测试统计结果
	auto 		[number]:性能测试,number为测试号码数量 默认100
	`[1:])

}

//命令补全
var completer = readline.NewPrefixCompleter(

	readline.PcItem("help"),
	readline.PcItem("notify"),
	readline.PcItem("callback"),

	readline.PcItem("outcall",
		readline.PcItem("yuyin"),
	),

	readline.PcItem("setcaller"),
	readline.PcItem("setasr"),
	readline.PcItem("setdelay"),
	readline.PcItem("setRecord"),
	readline.PcItem("settts"),
	readline.PcItem("stopPlay"),

	readline.PcItem("rtm"),
	readline.PcItem("transfer"),
	readline.PcItem("asrCall"),
	readline.PcItem("ttsCall"),
	readline.PcItem("show"),
	readline.PcItem("auto"),
	readline.PcItem("statics"),
	readline.PcItem("clear"),
)

func console_main() {

	l, err := readline.NewEx(&readline.Config{
		Prompt:       "\033[31m»\033[0m ",
		HistoryFile:  "/tmp/readline.tmp",
		AutoComplete: completer,
	})

	if err != nil {
		panic(err)
	}
	defer l.Close()

	log.SetOutput(l.Stderr())

	caller := globalConf.testNum
	number := globalConf.number
	for {
		line, err := l.Readline()
		if err != nil {
			break
		}

		args := strings.Split(line, " ")
		line = strings.TrimSpace(line)
		switch {

		case line == "help" || line == "?":
			usage(l.Stderr())
		case line == "show":
			fmt.Printf("caller:%v，called:%v\n", caller, number)
			fmt.Printf("asr switch：%v\n", globalConf.ai)
			fmt.Printf("auto call delay %v \n", globalConf.callDelay)

		case strings.HasPrefix(line, "notify"):
			testStr := ""
			if len(args) > 1 {
				testStr = args[1]
				fmt.Printf("呼叫通知测试;字符串:%s\n", testStr)
			}
			restsrv.TestCallNotify(globalConf.appId, number, caller, testStr)
		case strings.HasPrefix(line, "transfer"):

			restsrv.TestCallNotifyTransfer(globalConf.appId, number, caller,
				globalConf.number2, globalConf.timeOut)

		case strings.HasPrefix(line, "outcall"):

			if len(args) > 1 {
				_, err := strconv.Atoi(args[len(args)-1])
				if err == nil {
					number = args[len(args)-1]
					fmt.Printf("set new test number:%s\n", number)
				}
			}

			if strings.Contains(line, "yuyin") {
				restsrv.SetAnswerTypeYuyin()
			}

			callid := restsrv.TestOutCall(globalConf.appId, number, caller)
			globalConf.SetCallId(callid)

		case strings.HasPrefix(line, "asrCall"):

			restsrv.SetAnswerTypeDefault()
			if len(args) > 1 {
				number = args[1]
				fmt.Printf("set new test number:%s\n", number)
			}
			callid := restsrv.TestOutCallWithAi(globalConf.appId, number, caller)
			globalConf.SetCallId(callid)

		case strings.HasPrefix(line, "ttsCall"):
			if len(args) > 1 {
				number = args[1]
				fmt.Printf("set new test number:%s\n", number)
			}

			restsrv.SetAnswerTypeTts()
			callid := restsrv.TestOutCallWithAi(globalConf.appId, number, caller)
			globalConf.SetCallId(callid)

		case strings.HasPrefix(line, "setcaller"):
			if len(args) > 1 {
				caller = args[1]
				fmt.Printf("set new test number:%s\n", caller)
			}
		case strings.HasPrefix(line, "setasr"):
			if len(args) > 1 {
				if strings.HasPrefix(args[1], "true") {
					globalConf.ai = true
				} else {
					globalConf.ai = false
				}

				fmt.Printf("set ai switch:%v\n", globalConf.ai)
			}

		case strings.HasPrefix(line, "settts"):
			if len(args) > 1 {
				restsrv.SetTTSMrcp()
			} else {
				speed, err := strconv.ParseFloat(args[1], 10)
				if err != nil {
					restsrv.SetTTSSpeed(1.0)
				} else {
					restsrv.SetTTSSpeed(speed)
				}
			}

		case strings.HasPrefix(line, "setdelay"):
			if len(args) > 1 {
				t, err := strconv.Atoi(args[1])
				if err != nil {
					fmt.Println(err)
				} else {
					globalConf.callDelay = t
				}
				fmt.Printf("set call delay:%v\n", globalConf.callDelay)
			}

			//性能测试相关
		case strings.HasPrefix(line, "auto"):

			calls := 100
			if len(args) >= 2 {
				t, err := strconv.Atoi(args[1])
				if err != nil {
					fmt.Println(err)
				} else {
					calls = t
				}
			}
			//性能测试关闭回调打印
			globalConf.debug = false
			restsrv.DebugOff()

			fmt.Printf("start auto call:%v", calls)
			testCallAlways(calls, caller)

		case strings.HasPrefix(line, "rtm"):

			restsrv.SetAnswerTypertm()
			callid := restsrv.TestOutCall(globalConf.appId, number, caller)
			globalConf.SetCallId(callid)

		case strings.HasPrefix(line, "setRecord"):
			if len(args) >= 2 {
				restsrv.SetRecordTypeToMp3()
			} else {
				restsrv.SetRecordTypeToDefault()
			}

		case strings.HasPrefix(line, "stopPlay"):

			restsrv.TestStopPlay(globalConf.appId, globalConf.GetCallId())

		case strings.HasPrefix(line, "statics"):
			restsrv.ShowStatics()
		case strings.HasPrefix(line, "clear"):
			restsrv.ClearStatics()
		default:
			if len(line) > 0 {
				usage(l.Stderr())
			}
		}
	}
}
